#include "CRollerSqlOper.h"
#include "CPublicData.h"
#include "CRollerData.h"
QMap<QString, CRollerData*> selectDeviceInfo( QString& sErr)
{
	QMap<QString, CRollerData*> mapRollerData;
	CDatabaseExecData cData;
	cData.sTableName = "proller_device_info";
	cData.sSql = "select * from proller_device_info";
	cData.vecField.clear();
	cData.vecValue.clear();
	//cData.vecField.push_back("ID");
	//cData.vecValue.push_back(sId);
	bool bRet = CPublicData::instance().m_pSqlDllOper->exec(cData);
	sErr = cData.sErr;
	QVector<QSqlRecord> vecDataInfo = cData.vecRecord;
	for (int i = 0; i < vecDataInfo.size(); i++)
	{
		CRollerData* pRollerData = new CRollerData();
		pRollerData->m_sId = vecDataInfo[i].value("ID").toString().trimmed();
		pRollerData->m_sName = vecDataInfo[i].value("NANE").toString().trimmed();
		pRollerData->m_nRollerNo = vecDataInfo[i].value("ROLLERNO").toString().trimmed().toInt();
		pRollerData->m_sGuid = vecDataInfo[i].value("GUID").toString().trimmed();
		pRollerData->m_sDevAddr = vecDataInfo[i].value("DEV_ADDR").toString().trimmed();
		pRollerData->m_sRunAddr = vecDataInfo[i].value("RUN_POINT_ADDR").toString().trimmed();
		pRollerData->m_sPosAddr = vecDataInfo[i].value("POSITION_ADDR").toString().trimmed();
		pRollerData->m_sTempAddr = vecDataInfo[i].value("TEMPERATURE_ADDR").toString().trimmed();
		pRollerData->m_sVoiceAddr = vecDataInfo[i].value("VOICE_ADDR").toString().trimmed();
		pRollerData->m_sImgAddr = vecDataInfo[i].value("IMAGE_ADDR").toString().trimmed();
		pRollerData->m_sDetectAddr = vecDataInfo[i].value("DETECT_POSITION_ADDR").toString().trimmed();
		pRollerData->m_sRollerNoAddr = vecDataInfo[i].value("ROLLERNO_ADDR").toString().trimmed();
		pRollerData->m_sRollerNameAddr = vecDataInfo[i].value("ROLLERNAME_ADDR").toString().trimmed();
		pRollerData->m_dPosVal = vecDataInfo[i].value("POSITION_VAL").toDouble();
		pRollerData->m_sDetectPosVal = vecDataInfo[i].value("DETECT_POSITION_VAL").toString();
		pRollerData->m_dPosOffset = vecDataInfo[i].value("POSITION_OFFSET").toDouble();
		pRollerData->m_dTempL = vecDataInfo[i].value("DATA_TEMP_L").toDouble();
		pRollerData->m_dTempLL = vecDataInfo[i].value("DATA_TEMP_LL").toDouble();
		pRollerData->m_dTempH = vecDataInfo[i].value("DATA_TEMP_H").toDouble();
		pRollerData->m_dTempHH = vecDataInfo[i].value("DATA_TEMP_HH").toDouble();
		pRollerData->m_dVoiceL = vecDataInfo[i].value("DATA_VOICE_L").toDouble();
		pRollerData->m_dVoiceLL = vecDataInfo[i].value("DATA_VOICE_LL").toDouble();
		pRollerData->m_dVoiceH = vecDataInfo[i].value("DATA_VOICE_H").toDouble();
		pRollerData->m_dVoiceHH = vecDataInfo[i].value("DATA_VOICE_HH").toDouble();
		mapRollerData.insert(pRollerData->m_sId, pRollerData);
	}
	return mapRollerData;
}

bool insertDeviceInfo(CRollerData* pRollerData)
{
	CDatabaseExecData cData;
	cData.sTableName = "proller_device_info";
	cData.sSql = "INSERT INTO proller_device_info (GUID,ID,NANE,ROLLERNO,DEV_ADDR,RUN_POINT_ADDR,POSITION_ADDR,TEMPERATURE_ADDR,VOICE_ADDR,IMAGE_ADDR,DETECT_POSITION_ADDR,ROLLERNO_ADDR,ROLLERNAME_ADDR,POSITION_VAL,DETECT_POSITION_VAL,POSITION_OFFSET,DATA_TEMP_HH,DATA_TEMP_H,DATA_TEMP_L,DATA_TEMP_LL,DATA_VOICE_HH,DATA_VOICE_H,DATA_VOICE_L,DATA_VOICE_LL)"
		"values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
	cData.addValue("GUID", pRollerData->m_sGuid);
	cData.addValue("ID", pRollerData->m_sId);
	cData.addValue("NANE", pRollerData->m_sName);
	cData.addValue("ROLLERNO", QString::number(pRollerData->m_nRollerNo));
	cData.addValue("DEV_ADDR", pRollerData->m_sDevAddr);
	cData.addValue("RUN_POINT_ADDR", pRollerData->m_sDevAddr);
	cData.addValue("POSITION_ADDR", pRollerData->m_sPosAddr);
	cData.addValue("TEMPERATURE_ADDR", pRollerData->m_sTempAddr);
	cData.addValue("VOICE_ADDR", pRollerData->m_sVoiceAddr);
	cData.addValue("IMAGE_ADDR", pRollerData->m_sImgAddr);
	cData.addValue("DETECT_POSITION_ADDR", pRollerData->m_sDetectAddr);
	cData.addValue("ROLLERNO_ADDR", pRollerData->m_sRollerNoAddr);
	cData.addValue("ROLLERNAME_ADDR", pRollerData->m_sRollerNameAddr);
	cData.addValue("POSITION_VAL", QString::number(pRollerData->m_dPosVal));
	cData.addValue("DETECT_POSITION_VAL", pRollerData->m_sDetectPosVal);
	cData.addValue("POSITION_OFFSET", QString::number(pRollerData->m_dPosOffset));
	cData.addValue("DATA_TEMP_HH", QString::number(pRollerData->m_dTempHH));
	cData.addValue("DATA_TEMP_H", QString::number(pRollerData->m_dTempH));
	cData.addValue("DATA_TEMP_L", QString::number(pRollerData->m_dTempL));
	cData.addValue("DATA_TEMP_LL", QString::number(pRollerData->m_dTempLL));
	cData.addValue("DATA_VOICE_HH", QString::number(pRollerData->m_dVoiceHH));
	cData.addValue("DATA_VOICE_H", QString::number(pRollerData->m_dVoiceH));
	cData.addValue("DATA_VOICE_L", QString::number(pRollerData->m_dVoiceL));
	cData.addValue("DATA_VOICE_LL", QString::number(pRollerData->m_dVoiceLL));
	return CPublicData::instance().m_pSqlDllOper->exec(cData);
}

bool updataDeviceInfo(CRollerData* pRollerData)
{
	CDatabaseExecData cData;
	cData.vecField.clear();
	cData.vecValue.clear();
	cData.sTableName = "proller_device_info";
	cData.sSql = "update proller_device_info set ID=?, NANE=?,ROLLERNO=?,DEV_ADDR=?,RUN_POINT_ADDR=?,POSITION_ADDR=?,TEMPERATURE_ADDR=?,VOICE_ADDR=?,IMAGE_ADDR=?,DETECT_POSITION_ADDR=?,ROLLERNO_ADDR=?,ROLLERNAME_ADDR=?,POSITION_VAL=?,DETECT_POSITION_VAL=?,POSITION_OFFSET=?,DATA_TEMP_HH=?,DATA_TEMP_H=?,DATA_TEMP_L=?,DATA_TEMP_LL=?,DATA_VOICE_HH=?,DATA_VOICE_H=?,DATA_VOICE_L=?,DATA_VOICE_LL=?"
	" where GUID=?";
	cData.addValue("ID", pRollerData->m_sId);
	cData.addValue("NANE", pRollerData->m_sName);
	cData.addValue("ROLLERNO", QString::number(pRollerData->m_nRollerNo));
	cData.addValue("DEV_ADDR", pRollerData->m_sDevAddr);
	cData.addValue("RUN_POINT_ADDR", pRollerData->m_sDevAddr);
	cData.addValue("POSITION_ADDR", pRollerData->m_sPosAddr);
	cData.addValue("TEMPERATURE_ADDR", pRollerData->m_sTempAddr);
	cData.addValue("VOICE_ADDR", pRollerData->m_sVoiceAddr);
	cData.addValue("IMAGE_ADDR", pRollerData->m_sImgAddr);
	cData.addValue("DETECT_POSITION_ADDR", pRollerData->m_sDetectAddr);
	cData.addValue("ROLLERNO_ADDR", pRollerData->m_sRollerNoAddr);
	cData.addValue("ROLLERNAME_ADDR", pRollerData->m_sRollerNameAddr);
	cData.addValue("POSITION_VAL", QString::number(pRollerData->m_dPosVal));
	cData.addValue("DETECT_POSITION_VAL", pRollerData->m_sDetectPosVal);
	cData.addValue("POSITION_OFFSET", QString::number(pRollerData->m_dPosOffset));
	cData.addValue("DATA_TEMP_HH", QString::number(pRollerData->m_dTempHH));
	cData.addValue("DATA_TEMP_H", QString::number(pRollerData->m_dTempH));
	cData.addValue("DATA_TEMP_L", QString::number(pRollerData->m_dTempL));
	cData.addValue("DATA_TEMP_LL", QString::number(pRollerData->m_dTempLL));
	cData.addValue("DATA_VOICE_HH", QString::number(pRollerData->m_dVoiceHH));
	cData.addValue("DATA_VOICE_H", QString::number(pRollerData->m_dVoiceH));
	cData.addValue("DATA_VOICE_L", QString::number(pRollerData->m_dVoiceL));
	cData.addValue("DATA_VOICE_LL", QString::number(pRollerData->m_dVoiceLL));
	cData.addValue("GUID", pRollerData->m_sGuid);
	bool bRet= CPublicData::instance().m_pSqlDllOper->exec(cData);
	return bRet;
}

bool deleteDeviceInfo(QString sId)
{
	CDatabaseExecData cData;
	cData.sTableName = "proller_device_info";
	cData.sSql = "delete from proller_device_info where ID=?";
	cData.vecField.clear();
	cData.vecValue.clear();
	cData.vecValue.push_back(sId);
	cData.vecField.push_back("ID");
	bool bRet = CPublicData::instance().m_pSqlDllOper->exec(cData);
	return bRet;
}
